package com.springboot.util.img.seal;

import org.apache.commons.lang.StringUtils;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description
 * @Author zyj
 * @Date 10:18 2020/4/21
 */
public class SealSample {

//    public static void main(String[] args) throws Exception {
//        officialSeal("中国四大天王股份有限公司");
//        officialSeal_3();
        //OfficialSeal_1();
//        officialSeal_4("资产管理有限责任公司");
        //privateSeal_4("周杰伦");
//        ImageUtil imageUtil = new ImageUtil();
//        String name = "四川责任有限公司";
//        BufferedImage image = imageUtil.mergeImg( privateSeal_3("周杰伦"),officialSeal_4(name), 420, -140);
//        File outputfile = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹\\印章.png");
//        ImageIO.write(image, "png", outputfile);
//        String name = "";
//        for (int i = 0; i < 20; i++) {
//            name += "传";
//            officialSeal_4(name);
//        }
//        officialSeal_4(name);
//    }

    /**
     * @Description: 生成方章
     * @Param: [legalName 法人名称]
     * @return: java.awt.image.BufferedImage
     * @Author: zyj
     * @Date: 2020/9/18
     */
    public static BufferedImage privateSeal_5(String legalName) throws Exception {
        if (StringUtils.isEmpty(legalName)) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (StringUtils.isNotEmpty(legalName)) {
            Integer size;
            if (legalName.length() <= 4) {
                size = 28 * 4 / legalName.length();
            } else {
                size = 28;
            }
            java.util.List<String> textList = new ArrayList<>();
            if (StringUtils.isNotEmpty(legalName) && legalName.length() == legalName.getBytes().length) {
                //处理英文名字
                String text = legalName;
                String[] textArr = null;
                //使用“·”拆分
                if (text.indexOf("·") != -1) {
                    List<String> list = new ArrayList<>();
                    textArr = text.split("·");
                    for (String s : textArr) {
                        //去除首尾空格
                        list.add(s.trim());
                    }
                    for (String s : list) {
                        //使用“ ”拆分
                        if (s.indexOf(" ") != -1) {
                            String[] strs = s.split(" ");
                            for (String str : strs) {
                                textList.add(str);
                            }
                        } else {
                            textList.add(s);
                        }
                    }
                } else if (text.indexOf(" ") != -1) {//使用“ ”拆分
                    textArr = text.split(" ");
                    textList = Arrays.asList(textArr);
                } else {
                    textList.add(text);
                }
                //取最长的字符串来再次确定字体大小
                int lengthMax = textList.get(0).length();
                for (String s : textList) {
                    if (s.length() >= lengthMax) {
                        lengthMax = s.length();
                    }
                }
//            if(lengthMax>8){
//                size  = size*lengthMax/8;
//            }
                if (lengthMax < 8 && textList.size() > 3) {
                    size = size / 3 * textList.size();
                }
                if (textList.size() > 4) {
                    throw new IllegalArgumentException("英文名太长，只支持4段");
                }
            } else {
                if (legalName.length() < 2 || legalName.length() > 12) {
                    throw new IllegalArgumentException("中文名太长，只支持2-12个字");
                }
            }

            BufferedImage draw = Seal.builder()
                    .size(144)
                    .borderSquare(18)
                    .mainFont(SealFont.builder().text(legalName).size(size).space(8.50).build())
                    .build()
                    .drawCustSeal();
            return draw;
        }
        return null;
    }

    public static BufferedImage officialSeal(String name) throws Exception {

        BufferedImage draw = Seal.builder()
                .size(300)
                .borderCircle(SealCircle.builder().line(5).width(140).height(140).build())
                .mainFont(SealFont.builder().text(name).size(40).space(40.0).margin(12).build())
                .centerFont(SealFont.builder().text("★").size(80).build())
                .build()
                .draw();

        return draw;
    }

    public static void OfficialSeal_1() throws Exception {
        Seal.builder()
                .size(200)
                .borderCircle(SealCircle.builder().line(4).width(95).height(95).build())
                .mainFont(SealFont.builder().text("中国四大天王股份有限公司").size(24).space(24.0).margin(5).build())
                .centerFont(SealFont.builder().text("★").size(60).build())
                .titleFont(SealFont.builder().text("").size(0).space(0.0).margin(0).build())
                .build()
                .draw("F:\\桌面\\三方接口\\签章\\新建文件夹\\公章2.png");
    }


    public static void officialSeal_2() throws Exception {
        Seal.builder()
                .size(300)
                .borderCircle(SealCircle.builder().line(5).width(140).height(140).build())
                .mainFont(SealFont.builder().text("中国四大天王股份有限公司").size(35).space(35.0).margin(10).build())
                .centerFont(SealFont.builder().text("★").size(100).build())
                .titleFont(SealFont.builder().text("").size(0).space(0.0).margin(0).build())
                .build()
                .draw("F:\\桌面\\三方接口\\签章\\新建文件夹\\公章2.png");
    }


    public static void officialSeal_3() throws Exception {
        Seal.builder()
                .size(300)
                .borderCircle(SealCircle.builder().line(3).width(144).height(100).build())
                .borderInnerCircle(SealCircle.builder().line(1).width(140).height(96).build())
                .mainFont(SealFont.builder().text("中国四大天王股份有限公司").size(25).space(12.0).margin(10).build())
                .centerFont(SealFont.builder().text("NO.5201314").size(20).build())
                .titleFont(SealFont.builder().text("专用章").size(20).space(9.0).margin(64).build())
                .build()
                .draw("F:\\桌面\\三方接口\\签章\\新建文件夹\\公章3.png");
    }

    public static void privateSeal_1() throws Exception {
        Seal.builder()
                .size(300)
                .borderSquare(16)
                .mainFont(SealFont.builder().text("刘德").size(120).build())
                .build()
                .draw("F:\\桌面\\三方接口\\签章\\新建文件夹\\私章1.png");
    }

    public static void privateSeal_2() throws Exception {
        Seal.builder()
                .size(300)
                .borderSquare(16)
                .mainFont(SealFont.builder().text("刘德华印").size(120).build())
                .build()
                .draw("F:\\桌面\\三方接口\\签章\\新建文件夹\\私章2.png");
    }

    /**
     * @Description: 生成法人方章
     * @Param: [legalName 法人名]
     * @return: java.awt.image.BufferedImage
     * @Author: zyj
     * @Date: 2020/8/31
     */
    public static BufferedImage privateSeal_3(String legalName) throws Exception {
        BufferedImage image = Seal.builder()
                .size(140)
                .borderSquare(10)
                .mainFont(SealFont.builder().text(legalName).size(50).build())
                .build()
                .draw();
        return image;
    }

    public static void privateSeal_4(String legalName) throws Exception {
        Seal.builder()
                .size(300)
                .borderSquare(16)
                .mainFont(SealFont.builder().text(legalName + "印").size(120).build())
                .build()
                .draw("C:\\Users\\Administrator\\Desktop\\新建文件夹\\方章.png");
    }

    public static BufferedImage officialSeal_4(String name) throws Exception {
//        BufferedImage image = Seal.builder()
//                .size(420)
//                .borderCircle(SealCircle.builder().line(5).width(200).height(200).build())
//                .mainFont(SealFont.builder().text(name).size(40 * 14 / name.length() ).space(40.0*16/(name.length()-1)).margin(12).build())
//                .centerFont(SealFont.builder().text("★").size(114).build())
//                .build()
//                .draw();
//        System.out.println("字数："+name.length());
//        System.out.println(i);
//        String s = String.valueOf(v);
//        if(s.contains(".")){
//            s = s.substring(0,s.indexOf("."));
//        }
//        v = Double.valueOf(s);
//        System.out.println(v);
        int i = 40 * 14 / name.length()*11/8;
        double v = 40.0 * 16 / (name.length() - 1);
        if (name.length() < 10) {
            i = 56;
        }
        if (name.length() < 6) {
            i = 80;
            v = 120.0;
        }
        if (name.length() > 12) {
            v += 5;
        }
        BufferedImage image = Seal.builder()
                .size(420)
                .borderCircle(SealCircle.builder().line(10).width(200).height(200).build())
                .mainFont(SealFont.builder().text(name).size(i).space(v).margin(12).bold(true).build())
                .centerFont(SealFont.builder().text("★").size(114).build())
                .build()
                .draw();
        return image;
    }
}
